import numpy as np
import csv
import random
from datetime import datetime
from time import strftime

'''
to calculate the false nagetive, false positive

revision of test18
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_unit_list(cancerFN):
    sKey = np.array([])
    #read contiguity file
    fn = unitCSV

    #print "Strat building csv lists..."

    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        temp = [int(float(record["ID"])), int(float(record[cancerFN])),int(float(record[popFN])),float(record["Area"]), 0]
        #print temp
        sKey =np.append(sKey, temp)
    sKey.shape=(-1,5)
    return sKey

def build_region_list(inputCSV):
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    i = 0
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        unit_attri[i,4] = int(float(record[ra.fieldnames[-1]]))
        i = i + 1


def find_same_region(regionID):
    # return the original item id list within a region 
    iLen = unit_attri.shape
    i = 0
    temp = []
    while i < iLen[0]:
        if unit_attri[i,4] == regionID:
            temp = np.append(temp, unit_attri[i,0])
        i = i + 1
    return temp

def cal_region_attri(regionList):
    # calculate rate = cancer/pop for each region
    # return a list
    iLen = regionList.shape
    temp_reg_attri = np.array([])
    #temp_reg_cancer = np.array([])
    #temp_reg_pop = np.array([])

    i = 0
    while i < iLen[0]:
        temp = find_same_region(regionList[i])
        temp_cancer = 0
        temp_pop = 0
        temp_area = 0
        for item in temp:
            temp_cancer = temp_cancer + unit_attri[item, 1]
            temp_pop = temp_pop + unit_attri[item, 2]
            temp_area = temp_area + unit_attri[item, 3]
        temp_reg_attri = np.append(temp_reg_attri, [regionList[i], temp_cancer, temp_pop, temp_cancer/temp_pop, temp_area])
        i = i + 1

    temp_reg_attri.shape = (-1, 5)
    return temp_reg_attri
    #return temp_reg_rate, temp_reg_pop
    
def find_rep_region(unitList, listID):
    temp = np.array([])    
    for item in unitList:
        temp = np.append(temp, int(unit_attri[item,4]))

    dis_region = np.unique(temp) # distinct region ID in the unitList
    reg_riskarea_attri = np.array([])  #[[region_ID, cancer, pop, area]]
    temp_type_two_error = np.array([])

    '''
    unit_attri = [FID, cancer, pop, area, regionID]
    riskarea_attri = [total_cancer, total_pop, total_area]
    reg_attri = [region_ID, caner, pop, rate, area]
    * reg_riskarea_attri = [region_ID, cancer, pop, area]
    '''   
    
    for region in dis_region:
        reg_riskarea_attri = np.append(reg_riskarea_attri, [region, -1, -1, -1])
    reg_riskarea_attri.shape = (-1, 4)

    i = 0        
    for region in reg_riskarea_attri[:,0]:
        temp_pop = 0 
        temp_cancer = 0 
        temp_area = 0
        for unit in unitList:
            if int(unit_attri[unit, 4]) == int(region):
                temp_pop = temp_pop + unit_attri[unit, 2]
                temp_cancer = temp_cancer +  unit_attri[unit, 1]
                temp_area = temp_area + unit_attri[unit, 3]        
        reg_riskarea_attri[i, 1] = temp_cancer
        reg_riskarea_attri[i, 2] = temp_pop
        reg_riskarea_attri[i, 3] = temp_area
        i = i + 1
    temp_rep_id = np.array([])

    '''
    unit_attri = [FID, cancer, pop, area, regionID]
    riskarea_attri = [total_cancer, total_pop, total_area]
    reg_attri = [region_ID, caner, pop, rate, area]
    * reg_riskarea_attri = [region_ID, cancer, pop, area]
    '''
    falseNegative = 0
    falsePositive = 0
    detectedTruth = 0

    temp_total_pop = 0    

    iLen = dis_region.shape    

    if returnID == 0: # pop
        i = 0
        while i < iLen[0]:
            if reg_riskarea_attri[i,2]/riskarea_attri[listID-1, 1] > 0.5 or reg_riskarea_attri[i,2]/reg_attri[reg_riskarea_attri[i,0],2] > 0.5:  
                temp_rep_id = np.append(temp_rep_id, i)
                detectedTruth = detectedTruth + reg_riskarea_attri[i,2]
                temp_total_pop = temp_total_pop + reg_attri[reg_riskarea_attri[i,0],2]
            i = i + 1
        falsePositive = riskarea_attri[listID-1, 1] - detectedTruth
        falseNegative = temp_total_pop - detectedTruth
    elif returnID == 1: # cancer
        i = 0
        while i < iLen[0]:
            if reg_riskarea_attri[i,1]/riskarea_attri[listID-1,0] > 0.5 or reg_riskarea_attri[i,1]/reg_attri[reg_riskarea_attri[i,0],1] > 0.5: 
                temp_rep_id = np.append(temp_rep_id, i)
                detectedTruth = detectedTruth + reg_riskarea_attri[i,1]
                temp_total_pop = temp_total_pop + reg_attri[reg_riskarea_attri[i,0],1]
            i = i + 1
        falsePositive = riskarea_attri[listID-1, 0] - detectedTruth
        falseNegative = temp_total_pop - detectedTruth
    elif returnID == 2: # area
        i = 0
        while i < iLen[0]:
            if reg_riskarea_attri[i,3]/riskarea_attri[listID-1,2] > 0.5 or reg_riskarea_attri[i,1]/reg_attri[reg_riskarea_attri[i,0],1] > 0.5:  
                temp_rep_id = np.append(temp_rep_id, i)
                detectedTruth = detectedTruth + reg_riskarea_attri[i,3]
                temp_total_pop = temp_total_pop + reg_attri[reg_riskarea_attri[i,0],4]
            i = i + 1
        falsePositive = riskarea_attri[listID-1, 2] - detectedTruth
        falseNegative = temp_total_pop - detectedTruth
    temp_rep_id = np.unique(temp_rep_id)
    

    if temp_rep_id.shape[0] == 0:
        temp_type_two_error = np.append(temp_type_two_error, [repeat, listID, returnID])
        
    temp_reg_id = []
    for item in temp_rep_id:
        temp_reg_id.append(int(dis_region[item]))

    temp_DFF = np.array([detectedTruth, falsePositive, falseNegative])        
    return temp_reg_id, temp_type_two_error, temp_DFF
    
def find_max_id(list):
    # return the id of the maximum value
    i = 0
    temp_max = 0
    temp_max_id = 0
    
    for item in list:
        if item > temp_max:
            temp_max = item
            temp_max_id = i
        i = i + 1
    return temp_max_id           

def find_min_id(list):
    # return the id of the maximum value
    i = 0
    temp_min = 1000000
    temp_min_id = 0
    
    for item in list:
        if item < temp_min:
            temp_min = item
            temp_min_id = i
        i = i + 1
    return temp_min_id

def calMeasure():


    high_risk_region = np.array([])
    low_risk_regioin = np.array([])
    #tempPopCancer = np.array([])
    tempC_type_two_error = np.array([])
    tempC_high_measure = np.array([])
    tempC_low_measure = np.array([])
    
    tempListID = 1 
    tempID, temp_type_two_error, temp_mearsure = find_rep_region(H1, tempListID)
    high_risk_region = np.append(high_risk_region, tempID)
    #temp_mearsure [detectedTruth, falsePositive, falseNegative]
    tempC_high_measure = np.append(tempC_high_measure, temp_mearsure)
    tempC_type_two_error = np.append(tempC_type_two_error, temp_type_two_error)
    
    tempListID = 2
    tempID, temp_type_two_error, temp_mearsure = find_rep_region(L2, tempListID)
    low_risk_regioin = np.append(low_risk_regioin, tempID)
    #tempPopCancer = np.append(tempPopCancer, [tempTotalPop, tempTotalCancer, tempRepPop, tempRepCancer])
    tempC_low_measure = np.append(tempC_low_measure, temp_mearsure)
    tempC_type_two_error = np.append(tempC_type_two_error, temp_type_two_error)
    
    tempListID = 3
    tempID, temp_type_two_error, temp_mearsure = find_rep_region(H3, tempListID)    
    high_risk_region = np.append(high_risk_region, tempID)
    #tempPopCancer = np.append(tempPopCancer, [tempTotalPop, tempTotalCancer, tempRepPop, tempRepCancer])
    tempC_high_measure = np.append(tempC_high_measure, temp_mearsure)
    tempC_type_two_error = np.append(tempC_type_two_error, temp_type_two_error)
    
    tempListID = 4
    tempID, temp_type_two_error, temp_mearsure = find_rep_region(H4, tempListID)   
    high_risk_region = np.append(high_risk_region, tempID)
    #tempPopCancer = np.append(tempPopCancer, [tempTotalPop, tempTotalCancer, tempRepPop, tempRepCancer])
    tempC_high_measure = np.append(tempC_high_measure, temp_mearsure)
    tempC_type_two_error = np.append(tempC_type_two_error, temp_type_two_error)
    
    tempListID = 5
    tempID, temp_type_two_error, temp_mearsure = find_rep_region(L5, tempListID)
    low_risk_regioin = np.append(low_risk_regioin, tempID)
    #tempPopCancer = np.append(tempPopCancer, [tempTotalPop, tempTotalCancer, tempRepPop, tempRepCancer])
    tempC_low_measure = np.append(tempC_low_measure, temp_mearsure)
    tempC_type_two_error = np.append(tempC_type_two_error, temp_type_two_error)
    
    tempListID = 6    
    tempID, temp_type_two_error, temp_mearsure = find_rep_region(L6, tempListID)    
    low_risk_regioin = np.append(low_risk_regioin, tempID)
    #tempPopCancer = np.append(tempPopCancer, [tempTotalPop, tempTotalCancer, tempRepPop, tempRepCancer])
    tempC_low_measure = np.append(tempC_low_measure, temp_mearsure)
    tempC_type_two_error = np.append(tempC_type_two_error, temp_type_two_error)
    
    tempListID = 7    
    tempID, temp_type_two_error, temp_mearsure = find_rep_region(H7, tempListID)    
    high_risk_region = np.append(high_risk_region, tempID)
    #tempPopCancer = np.append(tempPopCancer, [tempTotalPop, tempTotalCancer, tempRepPop, tempRepCancer])
    tempC_high_measure = np.append(tempC_high_measure, temp_mearsure)
    tempC_type_two_error = np.append(tempC_type_two_error, temp_type_two_error)

    #high_rep_count = 4
    #low_rep_count = 3
    #print high_risk_region
    
    tempC_high_measure.shape = (-1, 3)
    tempC_low_measure.shape = (-1, 3)
    high_measure = tempC_high_measure.sum(axis=0)
    low_measure = tempC_low_measure.sum(axis=0)

    
    temp_high_rate = np.array([])
    for region in high_risk_region:
        temp_high_rate = np.append(temp_high_rate, [region, reg_attri[int(region), 3]])
    temp_high_rate.shape = (-1,2)

    temp_low_rate = np.array([])    
    for region in low_risk_regioin:
        temp_low_rate = np.append(temp_low_rate, [region, reg_attri[int(region), 3]])        
    temp_low_rate.shape = (-1,2)
    
    high_region_num = 0
    low_region_num = 0
    temp_total_high = 0
    temp_total_low = 0
        
    
    if temp_high_rate.shape[0] == 0:
        high_region_num = 999
        if temp_low_rate.shape[0] == 0:
            low_region_num = 999
            i = 0
            for rate in reg_attri[:,3]:
                if returnID == 0: # pop
                    temp_total_high = temp_total_high + reg_attri[i,2]
                    temp_total_low = temp_total_low + reg_attri[i,2]
                elif returnID == 1: # cancer
                    temp_total_high = temp_total_high + reg_attri[i,1]
                    temp_total_low = temp_total_low + reg_attri[i,1]
                elif returnID == 2: # area
                    temp_total_high = temp_total_high + reg_attri[i,4]
                    temp_total_low = temp_total_low + reg_attri[i,4]
                i = i + 1
        else:
            max_low_rate = temp_low_rate[find_max_id(temp_low_rate[:,1]), 1]
            i = 0
            for rate in reg_attri[:,3]:
                if rate <= max_low_rate:
                    low_region_num = low_region_num + 1
                    if returnID == 0: # pop
                        temp_total_low = temp_total_low + reg_attri[i,2]
                    elif returnID == 1: # cancer
                        temp_total_low = temp_total_low + reg_attri[i,1]
                    elif returnID == 2: # area
                        temp_total_low = temp_total_low + reg_attri[i,4]
                if returnID == 0: # pop
                    temp_total_high = temp_total_high + reg_attri[i,2]
                elif returnID == 1: # cancer
                    temp_total_high = temp_total_high + reg_attri[i,1]
                elif returnID == 2: # area
                    temp_total_high = temp_total_high + reg_attri[i,4]
                i = i + 1
                
    else:
        if temp_low_rate.shape[0] == 0:
            low_region_num = 999
            min_high_rate = temp_high_rate[find_min_id(temp_high_rate[:,1]), 1]
            i = 0
            for rate in reg_attri[:,3]:
                if rate >= min_high_rate:
                    high_region_num = high_region_num + 1
                    if returnID == 0: # pop
                        temp_total_high = temp_total_high + reg_attri[i,2]
                    elif returnID == 1: # cancer
                        temp_total_high = temp_total_high + reg_attri[i,1]
                    elif returnID == 2: # area
                        temp_total_high = temp_total_high + reg_attri[i,4]
                if returnID == 0: # pop
                    temp_total_low = temp_total_low + reg_attri[i,2]
                elif returnID == 1: # cancer
                    temp_total_low = temp_total_low + reg_attri[i,1]
                elif returnID == 2: # area
                    temp_total_low = temp_total_low + reg_attri[i,4]
                i = i + 1
        else:
            min_high_rate = temp_high_rate[find_min_id(temp_high_rate[:,1]), 1]
            max_low_rate = temp_low_rate[find_max_id(temp_low_rate[:,1]), 1]
            i = 0
            for rate in reg_attri[:,3]:
                if rate >= min_high_rate:
                    high_region_num = high_region_num + 1
                    # reg_attri = [region_ID, caner, pop, rate, area]
                    if returnID == 0: # pop
                        temp_total_high = temp_total_high + reg_attri[i,2]
                    elif returnID == 1: # cancer
                        temp_total_high = temp_total_high + reg_attri[i,1]
                    elif returnID == 2: # area
                        temp_total_high = temp_total_high + reg_attri[i,4]
                if rate <= max_low_rate:
                    low_region_num = low_region_num + 1
                    # reg_attri = [region_ID, caner, pop, rate, area]
                    if returnID == 0: # pop
                        temp_total_low = temp_total_low + reg_attri[i,2]
                    elif returnID == 1: # cancer
                        temp_total_low = temp_total_low + reg_attri[i,1]
                    elif returnID == 2: # area
                        temp_total_low = temp_total_low + reg_attri[i,4]
                i = i + 1

    # high_measure [detectedTruth, falsePositive, falseNegative]             
    high_measure = np.append(high_measure, temp_total_high - high_measure[0])
    low_measure = np.append(low_measure, temp_total_low - low_measure[0])

    high_measure = np.append(high_measure, low_measure)

    return high_risk_region.shape[0], high_region_num, low_risk_regioin.shape[0], low_region_num, tempC_type_two_error, high_measure

def cal_riskare_attri():
    temp_popcancer = np.array([])
    temp_pop, temp_cancer, temp_area = cal_list_pop(H1)
    temp_popcancer = np.append(temp_popcancer, [temp_pop, temp_cancer, temp_area])
    temp_pop, temp_cancer, temp_area = cal_list_pop(L2)
    temp_popcancer = np.append(temp_popcancer, [temp_pop, temp_cancer, temp_area])
    temp_pop, temp_cancer, temp_area = cal_list_pop(H3)
    temp_popcancer = np.append(temp_popcancer, [temp_pop, temp_cancer, temp_area])
    temp_pop, temp_cancer, temp_area = cal_list_pop(H4)
    temp_popcancer = np.append(temp_popcancer, [temp_pop, temp_cancer, temp_area])
    temp_pop, temp_cancer, temp_area = cal_list_pop(L5)
    temp_popcancer = np.append(temp_popcancer, [temp_pop, temp_cancer, temp_area])
    temp_pop, temp_cancer, temp_area = cal_list_pop(L6)
    temp_popcancer = np.append(temp_popcancer, [temp_pop, temp_cancer, temp_area])
    temp_pop, temp_cancer, temp_area = cal_list_pop(H7)
    temp_popcancer = np.append(temp_popcancer, [temp_pop, temp_cancer, temp_area])
    temp_popcancer.shape = (-1, 3)
    return temp_popcancer

def cal_list_pop(list):
    # calculate the total pop and cancer in the input list
    temp_pop = 0
    temp_cancer = 0
    temp_area = 0
    for item in list:
        temp_pop = temp_pop + unit_attri[item, 2]
        temp_cancer = temp_cancer + unit_attri[item, 1]
        temp_area = temp_area + unit_attri[item, 3]
    return temp_cancer, temp_pop, temp_area 

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    #inputs = get_inputs()
    print "begin at " + getCurTime()

    H1 = [8,16,844,915,919,921,923,924]
    L2 = [5,103,106,513,517,518,520,531,534,535,536,541]
    H3 = [63,265,267,268,333,336,337,339,340,342,343,348]
    H4 = [13,174,178,198,886,887,888,889,890]
    L5 = [146,171,182,810,811,814,815,864,867]
    L6 = [20,133,692,694,695,696,698,702,705]
    H7 = [69,70,87,88,369,370,372,442,443]
    
    unitCSV = "C:/TP1000_1m.csv"
    popFN = "pop"
    
    #filePath = "C:/_DATA/CancerData/test/Jan15/Thousand/10000/OO_CLK/"
    #filePath = "F:/OO_WARD/"
    j = 0
    while j < 1:
        if j == 0:
            soMethod = "OO"
        elif j == 1:
            soMethod = "SO"
        elif j == 2:
            soMethod = "SS"
        else:
            print "error!"
            break

        filePath = "C:/_DATA/CancerData/test/Jan15/Thousand/8000/"+ soMethod +"_WARD/"
        
        # returnID used in function find_rep_region
        returnID = 0  # 0: return max_pop, 1: return max_cancer, 2: return max_rate or min_rate, 3: return max_area
        
        while returnID < 1:
            type_two_error = np.array([])
            high_low_measure = np.array([])
            new_measure = np.array([])
            repeat = 179
            #PopCancer = np.array([])
            
            #while repeat < 1001:
            while repeat < 180:
                regionCSV = filePath + soMethod +"_WARDcancer"+ str(repeat) +".csv"
                sField_Ob = "cancer" + str(repeat)
                unit_attri = build_unit_list(sField_Ob) # [FID, cancer, pop, area, regionID]
                build_region_list(regionCSV)
                dis_reg = np.unique(unit_attri[:,4])
                riskarea_attri = cal_riskare_attri()  # [total_cancer, total_pop, total_area]
                reg_attri = cal_region_attri(dis_reg) # [region_ID, caner, pop, rate, area]
                np.savetxt("c:/temp/a.csv", riskarea_attri[:,0:2], delimiter=',')
                '''
                unit_attri = [FID, cancer, pop, area, regionID]
                riskarea_attri = [total_cancer, total_pop, total_area]
                reg_attri = [region_ID, caner, pop, rate, area]
                            
                temp_high_count, temp_high, temp_low_count, temp_low, temp_type_two_error_1, temp_mearsure = calMeasure()
                new_measure = np.append(new_measure, temp_mearsure)
                high_low_measure = np.append(high_low_measure, [temp_high_count, temp_high, temp_low_count, temp_low])
                type_two_error = np.append(type_two_error, temp_type_two_error_1)'''
                repeat = repeat + 1
            '''
            high_low_measure.shape = (-1,4)
            new_measure.shape = (-1,8)

            if returnID == 0:
                txtName_HLM = filePath + soMethod + "_Max_Pop_1.csv"
                txtName_PC = filePath + soMethod + "_Max_Pop_PopCancer_1.csv"
                txtNameTTE = filePath + soMethod + "_Max_Pop_type_two_error_1.csv"
                txtNameNM = filePath + soMethod + "_POP_newMeasure_1.csv"
            elif returnID == 1:
                txtName_HLM = filePath + soMethod + "_Max_Cancer_1.csv"
                txtName_PC = filePath + soMethod + "_Max_Cancer_PopCancer_1.csv"
                txtNameTTE = filePath + soMethod + "_Max_Cancer_type_two_error_1.csv"
                txtNameNM = filePath + soMethod + "_Cancer_newMeasure_1.csv"
            elif returnID == 2:
                txtName_HLM = filePath + soMethod + "_Max_Area_1.csv"
                txtName_PC = filePath + soMethod + "_Max_Area_PopCancer_1.csv"
                txtNameTTE = filePath + soMethod + "_Max_Area_type_two_error_1.csv"
                txtNameNM = filePath + soMethod + "_Area_newMeasure_1.csv"
            
            #print high_low_measure
            np.savetxt(txtName_HLM, high_low_measure, delimiter=',')
            #PopCancer.shape = (-1, 28)
            #print PopCancer
            #np.savetxt(txtName_PC, PopCancer, delimiter=',')
            type_two_error.shape = (-1, 3)
            print "type_two_error = ", type_two_error.shape[0]
            np.savetxt(txtNameTTE, type_two_error, delimiter=',')
            np.savetxt(txtNameNM, new_measure, delimiter=',')
            
            del type_two_error, high_low_measure, repeat, new_measure
            '''
            returnID = returnID + 1
        j = j + 1

    print "end at " + getCurTime()
    print "========================================================================"  
    #print "========================================================================"
    

    #print "temp_high_rate =", min_high_rate
    #print "temp_low_rate=", max_low_rate
    #print temp_high_rate, temp_low_rate
    #print high_risk_region, low_risk_regioin

    #output_results()  

           
